// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var sections = Array();
sections[0] = "Restaurant";
sections[1] = "Hotel";
sections[2] = "Resort";

/*
 * 검색 관련
 */
function search()
{
	var str = $("q").value;

	for( i = 0; i < sections.length; i++)
	{			
		eval("showResult" + sections[i] + "('" + str + "');");
	}
	return false;
}

// 음식점 검색
function showResultRestaurant(str)
{	
	var queryString = "?q=" + encodeURIComponent(str) + "";
	var myAjax = new Ajax.Updater('restaurant','/search_jeju/searchRestaurant' + queryString,
									 {
									 	method:"get",
										onCreate: function()
										{
									  		document.getElementById('restaurant').innerHTML =
											 "<p class='ajaxloader'>Loading 음식점...</p>";
									  	},
									 	asynchronous: true
									 }
								  );
}

// 숙박지 검색
function showResultHotel(str)
{
	var queryString = "?q=" + encodeURIComponent(str) + "";
	var myAjax = new Ajax.Updater('hotel','/search_jeju/searchHotel' + queryString,
									{
									 	method: "get",
										onCreate: function()
										{
									  		document.getElementById('hotel').innerHTML =
											"<p class='ajaxloader'>Loading 숙박지...</p>";
									  	},
									 	asynchronous: true
									 }
								  );
}

// 관광지 검색
function showResultResort(str)
{
	var queryString = "?q=" + encodeURIComponent(str) + "";
	var myAjax = new Ajax.Updater('resort','/search_jeju/searchResort' + queryString,
									{
									 	method: "get",
										onCreate: function()
										{
											document.getElementById('resort').innerHTML =
											 "<p class='ajaxloader'>Loading 관광지...</p>";
									  	},
									 	asynchronous: true
									 }
								  );
}

// 할인항공권 검색
function showResultDiscountAir(str)
{
	var queryString = "?q=" + encodeURIComponent(str) + "";
	
	var myAjax = new Ajax.Updater('discount','/search_jeju/searchDiscountAir' + queryString, {method:"get", asynchronous: true});

}

/*
 * 지도관련
 */

// 정보 출력을 위한 NInfoWindow 객체를 생성하여 등록합니다.
var mapObj;
var infowin;
var zoomControl;
var saveBtn;

function showNaverMap(mapx, mapy, zoom)
{
	if(!zoom) zoom = 3;
	mapObj = new NMap(document.getElementById('mapContainer'));
	infowin = new NInfoWindow();
	mapObj.addOverlay(infowin);
	
	mapObj.setCenterAndZoom(new NPoint(mapx, mapy), zoom);
	mapObj.enableWheelZoom();
 	
	// 줌 컨트롤 추가
	zoomControl = new NZoomControl();
	zoomControl.setAlign("left"); 
	zoomControl.setValign("bottom"); 
	mapObj.addControl(zoomControl);
	
	// 저장 버튼 추가
	saveBtn = new NSaveBtn();
	saveBtn.setAlign("right"); 
	saveBtn.setValign("bottom");
	mapObj.addControl(saveBtn);
}

function setMarker(pos, content)
{
	var iconUrl = 'http://sstatic.naver.com/search/local/icon3/ico_arrow.gif';
	var marker = new NMark(pos, new NIcon(iconUrl, new NSize(27, 39)));
	// marker 객체에 마우스가 오버했을 때, 실행할 함수를 등록합니다.
	NEvent.addListener(marker, "mouseover",
		function(pos){
			if(content == null)
				infowin.set(pos, getContent(new Array('제목', '전화번호', '주소')));
			else
				infowin.set(pos, getContent(content));
			infowin.showWindow();
			mapObj.removeControl(zoom);
		}
	);
	NEvent.addListener(marker, "mouseout",
		function(pos){
			infowin.hideWindow();
			mapObj.addControl(zoom);
		}
	);
	
	// 지도에 마커를 등록합니다.
	mapObj.addOverlay(marker);
}

function showNaverMapAndMark(mapx, mapy, zoom, content)
{	
	showNaverMap(mapx, mapy, zoom);
	setMarker(new NPoint(mapx, mapy), content);
}

function getContent(content)
{
	var title = content[0];
	var tel = content[1];
	var address = content[2];

	var body = ' \
	<div style="width:170px;border:solid 1px black; background:white; padding:5px 5px 5px 5px;"> \
	<div style="font-size:10pt;font-family:굴림, tahoma;font-weight:bold;float:left;">'+title+'</div> \
	<div style="font-size:8pt;font-family:굴림, tahoma;">'+tel+'</div> \
	<div style="font-size:8pt;font-family:굴림, tahoma; clear: both; text-align:left">'+address + '</div>';
	return body;
}

function initMarker(pos, count, content){
	var iconUrl = 'http://sstatic.naver.com/search/local/icon3/icos_L'+String.fromCharCode(64+count)+'.gif';
	// 마커 생성
	var marker = new NMark(pos, new NIcon(iconUrl, new NSize(16, 15)));
	// marker 객체에 마우스가 오버했을 때, 실행할 함수를 등록합니다.
	NEvent.addListener(marker, "mouseover",
		function(pos){
			infowin.set(pos, content);
			infowin.showWindow();
		}
	);

	// 지도에 마커를 등록합니다.
	mapObj.addOverlay(marker);
}

/*
 * 날씨관련
 */
function showResultWeather()
{
	var queryString = "?dayf=" + 1;
	
	var errFailure = function (request)
	{
		document.getElementById('weather').innerHTML = "Ajax error";
	}
	var myAjax = new Ajax.Updater('weather', '/search_jeju/searchWeather' + queryString,
						 {
						 	method: 'get',
						 	onFailure: errFailure,
							onCreate: function()
							{
								document.getElementById('weather').innerHTML =
								"<p class='ajaxloader'>Loading 날씨...</p>";
							},
							onComplete: function()
							{
								rotateWeather();
							},
							asynchronous: true
						}
					); 		
}
function showResultForecast()
{
	var queryString = "?dayf=" + 3;		
	var myAjax = new Ajax.Updater('forecast', '/search_jeju/searchForecast' + queryString,
									 {
									 	method: 'get',
										onCreate: function()
										{
											document.getElementById('forecast').innerHTML =
											 "<p class='ajaxloader'>Loading 일기예보...</p>";
									 	},
									 	asynchronous: true
									 }
								 );
}

var localNo = 0;
var numOfLocal = 3;
function rotateWeather()
{		
	localNo = localNo % numOfLocal; 
	localNo++;

	for(i = 1; i <= numOfLocal; i++)
	{
		//if(i==localNo) continue;
		hideLayer('divWeather' + i);
	}	
	showLayer('divWeather' + localNo);	
	
	setTimeout("rotateWeather()", 6000);
}

/*
 * 레이어 관련
 */
function showLayer(divName)
{
	var divObj = document.getElementById(divName);
	
	if(divObj != null)
	{
		Effect.Appear(divObj,{duration: 0.3});
	}
}

function hideLayer(divName)
{
	var divObj = document.getElementById(divName);
	
	if(divObj != null)
	{
		//Effect.Fade(divObj,{duration: 0.3});
		divObj.style.display = "none";	
	}
}

window.onload = function()
{
	showResultWeather();
	showResultForecast();
	showNaverMap(265000, 90000, 9);
};


